package com.fqgj.sentry.manage;


import com.fqgj.sentry.common.exception.BizException;
import com.fqgj.sentry.common.exception.enums.ErrorCodeEnums;
import com.fqgj.sentry.manage.dao.McGroupManagerDao;
import com.fqgj.sentry.manage.dao.McUserDao;
import com.fqgj.sentry.manage.entity.McUserEntity;
import com.fqgj.sentry.manage.enums.UserLevelEnums;
import com.fqgj.sentry.manage.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.HashMap;


/**
 * Created by ykpbean kangping.ying@yuntu-inc.com
 *
 * @description
 * @create 2017-07-12 下午2:25
 */

@Service
public class UserServiceImpl implements UserService {


    @Autowired
    private McUserDao mcUserDao;
    @Autowired
    private McGroupManagerDao mcGroupManagerDao;

    @Override
    public McUserEntity selectByAccount(String account) {
        McUserEntity mcUserEntity = mcUserDao.selectByAccount(account);
        if (null == mcUserEntity || mcUserEntity.getDeleted()) {
            throw new BizException(ErrorCodeEnums.user_not_exist_error);
        }
        return mcUserEntity;
    }

    @Override
    public McUserEntity selectById(Long userId) {
        McUserEntity mcUserEntity = mcUserDao.selectByPrimaryKey(userId);
        if (null == mcUserEntity || mcUserEntity.getDeleted()) {
            throw new BizException(ErrorCodeEnums.user_not_exist_error);
        }
        return mcUserEntity;
    }

    @Override
    public Page<McUserEntity> selectUserListForPage(String account, Long merchantId, Integer pageNum, Integer pageSize) {
        HashMap<String, Object> param = new HashMap<>();
        param.put("account", account);
        param.put("merchantId", merchantId);
        Page<McUserEntity> page = PageHelper.startPage(pageNum, pageSize);
        mcUserDao.selectUserList(param);
        return page;
    }

    @Override
    public McUserEntity insertUser(McUserEntity mcUserEntity) {
        McUserEntity entity = mcUserDao.selectByAccount(mcUserEntity.getAccount());
        if (null != entity) {
            throw new BizException(ErrorCodeEnums.user_is_exist_error);
        }
        return mcUserDao.insert(mcUserEntity);
    }

    @Override
    public Boolean deleteUser(Long userId, Long merchantId) {
        McUserEntity userEntity = mcUserDao.selectByPrimaryKey(userId);
        if (null == userEntity || userEntity.getDeleted()) {
            throw new BizException(ErrorCodeEnums.user_not_exist_error);
        }
        if (userEntity.getLevel().equals(UserLevelEnums.merchant_admin)) {
            throw new BizException(ErrorCodeEnums.merchant_level_error);
        }
        if (!userEntity.getMerchantId().equals(merchantId)) {
            throw new BizException(ErrorCodeEnums.merchant_user_error);
        }
        mcUserDao.deleteByPrimaryKey(userId);
        mcGroupManagerDao.deleteByUserId(userId);
        return true;
    }
}
